/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import dynamixel.Actuator;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author minaev
 */
public class WardrobeAnalyser {

    Actuator[] acts;
    int cur_speed = -1;
    int cur_torq = -1;
    int act_num = 0;
    int act_pos = 0;
    int act_speed = 0;
    int act_tor = 0;
    int act_scan = 0;
    final int max_cnt = 2048;
    int cur_cnt = 0;
    int tor[] = new int[max_cnt];
    long time[] = new long[max_cnt];
    int scan_act = 0;
    final int max_res = 10;
    int cnt_res = 0;
    int restor[][] = new int[max_res][];
    long restime[][] = new long[max_res][];
    boolean scanning = true;
    Thread scan = new Thread(new Runnable() {
        @Override
        public void run() {
            scanning = true;
            cur_cnt = 0;
            long start_time = System.currentTimeMillis();
            while (scanning) {
                if (cur_cnt < max_cnt) {
                    tor[cur_cnt] = acts[scan_act].getCurTorque();
                    time[cur_cnt] = System.currentTimeMillis() - start_time;
                    if (time[cur_cnt] > 5000) {
                        scanning = false;
                    }
                    if (tor[cur_cnt] != -1) {
                        cur_cnt++;
                    }
                }
            }
        }
    });

    public WardrobeAnalyser(Actuator[] acts) {
        this.acts = acts;
    }

    public void analys(String script) {
        StringTokenizer st = new StringTokenizer(script, "" + (char) 13 + (char) 10, false);
        while (st.countTokens() > 0) {
            analysLine(st.nextToken());
        }
    }

    public void analysLine(String scr_line) {
        StringTokenizer st = new StringTokenizer(scr_line, "\t ;", false);
        if (st.countTokens() >= 5) {
            act_num = Integer.valueOf(st.nextToken());
            act_pos = Integer.valueOf(st.nextToken());
            act_speed = Integer.valueOf(st.nextToken());
            act_tor = Integer.valueOf(st.nextToken());
            act_scan = Integer.valueOf(st.nextToken());

            acts[act_num].setSpeed(act_speed, true);
            acts[act_num].setTorqueLimit(act_tor);

            if (/*act_scan == 1*/false) {
                scan_act = act_num;
                acts[act_num].setGoalPosition(act_pos);
                {
                    scanning = true;
                    cur_cnt = 0;
                    long start_time = System.currentTimeMillis();
                    while (scanning) {
                        if (cur_cnt < max_cnt) {
                            tor[cur_cnt] = acts[scan_act].getCurTorque();
                            time[cur_cnt] = System.currentTimeMillis() - start_time;
                            if (time[cur_cnt] > 5000) {
                                scanning = false;
                            }
                            if (tor[cur_cnt] != -1) {
                                cur_cnt++;
                            }
                        }
                    }
                }
                restor[cnt_res] = new int[cur_cnt];
                restime[cnt_res] = new long[cur_cnt];
                System.arraycopy(tor, 0, restor[cnt_res], 0, cur_cnt);
                System.arraycopy(time, 0, restime[cnt_res], 0, cur_cnt);
                if (cnt_res < max_res) {
                    cnt_res++;
                }
            } else {
                acts[act_num].setGoalPosition(act_pos);
                try {
                    Thread.sleep(50);
                } catch (InterruptedException ex) {
                    Logger.getLogger(WardrobeAnalyser.class.getName()).log(Level.SEVERE, null, ex);
                }
                while (acts[act_num].isMoving()) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(WardrobeAnalyser.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }

        } else {
            System.out.println("Falied line: " + scr_line);
        }
    }

    public static void main(String[] args) {
    }
}
